package com.weme.common.util;


import android.content.Context;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class WholeTimeUtils {

    public static Long getCurrentTime() {
        return System.currentTimeMillis() / 1000;
    }

    public static String millis2String(long time, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US);
        return "\u200E"+ millis2String(time * 1000L, format)+"\u200E";
    }

    public static String simpleMillis2String(long time, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US);
        return millis2String(time * 1000L, format);
    }
    public static String millis2String(long time, SimpleDateFormat format){
        return  "";
    }

    public static String date2String(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US);
        return format.format(date);
    }

    // 计算到期时间,返回数组，[日, 时, 分, 秒]
    public static int[] getExpirationTime(long expire){
        long current = System.currentTimeMillis();
        if (current < expire * 1000){
            long time = expire * 1000 - current;
            int day = Math.round(time * 1.0f / 1000 / 60 / 60 / 24);
            // 时
            int hour = Math.round(time * 1.0f / 1000 / 60 / 60 % 24);
            // 分
            int minute = Math.round(time * 1.0f / 1000 / 60 % 60);
            // 秒
            int second = Math.round(time * 1.0f / 1000 % 60);
            return new int[]{day, hour, minute, second};
        }else {
            return new int[]{0,0,0,0};
        }
    }

    // 1v1聊天中用于比较两个消息时间是否大于5分钟
    public static Boolean getDiffer(Long startTime, Long endTime) {
        long differTime = endTime - startTime;
        //大于5分钟
        return differTime > 5 * 60;
    }

    public static String getChatTime(Context context, Long startTime) {
        long current = System.currentTimeMillis() / 1000;
        long start = startTime * 1000;
        long wee = getWeeOfToday();
        if (start >= wee) { // 今天内
            return "\u200E"+WholeTimeUtils.millis2String(startTime, "HH:mm")+"\u200E";
        } else if (current - startTime > 365L * 12 * 24 * 60 * 60){ // 显示年份
            return "\u200E"+WholeTimeUtils.millis2String(startTime, "yyyy/MM/dd HH:mm")+"\u200E";
        }else {
            return "\u200E"+WholeTimeUtils.millis2String(startTime, "MM-dd HH:mm")+"\u200E";
        }
    }

    public static long getWeeOfToday() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

}
